package com.hzlj.position.locate.common.dto.reportCommon;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.List;
import java.util.stream.Collectors;

@Data
@ToString
@NoArgsConstructor
public class ReportNoticeResultDTO {
    /**
     * 成功的记录
     */
    private List<NoticeResultDTO> successes = Lists.newArrayList();
    /**
     * 失败的记录
     */
    private List<NoticeResultDTO> fails = Lists.newArrayList();
    /**
     * 失败的描述
     */
    private String failsMsg;

    public ReportNoticeResultDTO(String failsMsg) {
        this.failsMsg = failsMsg;
    }


    public void addSuccess(String id, String jzId, String xm, String body) {
        successes.add(new NoticeResultDTO(id, jzId, xm, body));
    }

    public void addFail(String id, String jzId, String xm, String body) {
        fails.add(new NoticeResultDTO(id, jzId, xm, body));
    }

    public void addFail(String id, String jzId, String xm, String body, String failsMsg) {
        fails.add(new NoticeResultDTO(id, jzId, xm, body, failsMsg));
    }

    public String getFailsMsg() {
        if (ObjectUtil.isNotEmpty(failsMsg)) {
            return failsMsg;
        }
        if (ObjectUtil.isEmpty(fails)) {
            return null;
        }
        return String.format("【%s】发起点验通知失败，对方收不到点验通知",
                fails.stream().map(NoticeResultDTO::getXm).collect(Collectors.joining("，")));
    }

    @Data
    public static class NoticeResultDTO {
        private String id;
        private String jzId;
        private String xm;
        private String body;
        private String failsMsg;

        public NoticeResultDTO(String id, String jzId, String xm, String body) {
            this(id, jzId, xm, body, null);
        }

        public NoticeResultDTO(String id, String jzId, String xm, String body, String failsMsg) {
            this.id = id;
            this.jzId = jzId;
            this.xm = xm;
            this.body = body;
            this.failsMsg = failsMsg;
        }
    }
}
